import 'package:flutter/material.dart';
import 'package:flutter_billi/http/core/hi_error.dart';
import 'package:flutter_billi/http/dao/login_dao.dart';
import 'package:flutter_billi/navgator/hi_navigator.dart';
import 'package:flutter_billi/page/login_page.dart';
import 'package:flutter_billi/util/string_util.dart';
import 'package:flutter_billi/util/toast.dart';
import 'package:flutter_billi/widget/LoginButton.dart';
import 'package:flutter_billi/widget/appBar.dart';
import 'package:flutter_billi/widget/login_effect.dart';
import 'package:flutter_billi/widget/login_input.dart';

class RegistrationPage extends StatefulWidget {
  const RegistrationPage({Key key}) : super(key: key);

  @override
  _RegistrationPageState createState() => _RegistrationPageState();
}

class _RegistrationPageState extends State<RegistrationPage> {
  bool protect = false;
  bool loginEnable = false;

  String userName;
  String password;
  String rePassword;
  String imoocId;
  String orderId;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar("注册", "登陆", () {
        HiNavigator.getInstance().onJumpTo(RouteStatus.login);
      }),
      body: Container(
        child: ListView(
          children: [
            LoginEffect(
              protect: protect,
            ),
            LoginInput(
              "用户名",
              "请输入用户名",
              onChanged: (value) {
                userName = value;
                checkInput();
              },
            ),
            LoginInput(
              "密码",
              "请输入密码",
              lineStretch: true,
              obscureText: true,
              onChanged: (value) {
                password = value;
                checkInput();
              },
              focusChanged: (focu) {
                this.setState(() {
                  protect = focu;
                });
              },
            ),
            LoginInput(
              "请确认密码",
              "请再次输入密码",
              lineStretch: true,
              obscureText: true,
              onChanged: (value) {
                rePassword = value;
                checkInput();
              },
              focusChanged: (focu) {
                this.setState(() {
                  protect = focu;
                });
              },
            ),
            LoginInput(
              "慕课网id",
              "请输入慕课网id",
              keyboardType: TextInputType.number,
              onChanged: (value) {
                imoocId = value;
                checkInput();
              },
            ),
            LoginInput(
              "课程订单号",
              "请输入课程订单号",
              keyboardType: TextInputType.number,
              onChanged: (value) {
                orderId = value;
                checkInput();
              },
            ),
            Padding(
              padding: EdgeInsets.only(top: 20, left: 20, right: 20),
              child: LoginButton(
                title: "注册",
                enable: loginEnable,
                onPressed: checkParams,
              ),
            )
          ],
        ),
      ),
    );
  }

  void checkInput() {
    if (isNotEmpty(userName) &&
        isNotEmpty(password) &&
        isNotEmpty(rePassword) &&
        isNotEmpty(imoocId) &&
        isNotEmpty(orderId)) {
      this.setState(() {
        loginEnable = true;
      });
    }
  }

  void send() async {
    try {
      var result = await LoginDao.registration(userName, password, imoocId, orderId);
      print(result);
      if (result['code'] == 0) {
        // print("注册OK");
        showToast("注册成功");
        HiNavigator.getInstance().onJumpTo(RouteStatus.login);
      } else {
        print(result['msg']);
        showWarnToast(result['msg']);
      }
    } on NeedAuth catch (err) {
      print(err);
      showWarnToast(err.message);
    } on HiNetError catch (err) {
      print(err);
      showWarnToast(err.message);
    }
  }

  void checkParams() {
    String tips;
    if (rePassword != password) {
      tips = "前后密码不一致";
    } else if (orderId.length != 4) {
      tips = "请输入正确的订单号";
    }
    if (tips == null) {
      send();
    } else {
      print(tips);
      showWarnToast(tips);
      return;
    }
  }
}
